package cn.com.oceansoft.modules.message.controller;

import cn.com.oceansoft.application.Constant;
import cn.com.oceansoft.application.common.util.HttpClientUtil;
import cn.com.oceansoft.application.constant.ConfigInfo;
import cn.com.oceansoft.framework.kits.ApiResult;
import cn.com.oceansoft.modules.cache.service.BaseCacheService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Map;
import java.util.StringTokenizer;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by wzy on 2016/12/13.
 */
@Controller
@RequestMapping("messages")
public class MessageController {
    @Resource
    private BaseCacheService cacheService;
    @Resource
    protected ConfigInfo configInfo;

    @ResponseBody
    @RequestMapping(value = "/page", method = GET)
    public ApiResult page(HttpServletRequest request) {
        Map mp_current_member = (Map) request.getSession().getAttribute(Constant.CURRENT_MEMBER);
        String userGuid = mp_current_member.get("guid").toString();
        String ps = request.getParameter("ps");
        String pi = request.getParameter("pi");
        String result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "messages/page",
                "ps", ps, "pi", pi, "q_receiver_s_eq", userGuid, "q_CHANNLE_s_eq", "3");
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }


    @ResponseBody
    @RequestMapping(value = "/read", method = POST)
    public ApiResult read(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        int i = 0;
        StringTokenizer tokenizer = new StringTokenizer(ids, ",");

        String idsNew = "";

        while (tokenizer.hasMoreTokens()) {
            if (i == 0) {
                idsNew = idsNew + "'" + tokenizer.nextToken() + "'";
            } else {
                idsNew = idsNew + ",'" + tokenizer.nextToken() + "'";
            }
            i++;
        }
        idsNew="("+idsNew+")";
        Map<String,String> map =new HashedMap();
        map.put("msgId",idsNew);
        String result = HttpClientUtil.doPostString(configInfo.getMsHostUrl() + "messages/read",map);

        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }

    @ResponseBody
    @RequestMapping(value = "/del", method = POST)
    public ApiResult del(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        int i = 0;
        StringTokenizer tokenizer = new StringTokenizer(ids, ",");
        String idsNew = "";
        while (tokenizer.hasMoreTokens()) {
            if (i == 0) {
                idsNew = idsNew + "'" + tokenizer.nextToken() + "'";
            } else {
                idsNew = idsNew + ",'" + tokenizer.nextToken() + "'";
            }
            i++;
        }
        idsNew="("+idsNew+")";
        Map<String,String> map =new HashedMap();
        map.put("msgId",idsNew);
        String result = HttpClientUtil.doPostString(configInfo.getMsHostUrl() + "messages/del",map);
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }

}
